/*
MeeDocs - A Google Docs / Google Drive client for N9
Copyright 2012 Marcel D. Juhnke <marcel.juhnke@ovi.com>

This file is part of MeeDocs.

    MeeDocs is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    MeeDocs is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MeeDocs.  If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.0
import MeeDocs 1.0
import "meedocs.js" as Meedocs
import "storage.js" as Storage


Page {

    property string doc_info: ""
    property string doc_name: ""
    //property string addDocId: ""
    property alias busy_visible: listBusy.visible
    property alias busy_running: listBusy.running


    tools: docTools
    onStatusChanged: {
        if (status === PageStatus.Inactive)
             docView.doc_info = "";
    }


    MeeDocs {
        id: meeDocs

        onCollectionCreated: appWindow.pageStack.pop()
        onAddedToCollection: meeDocs.openShareUI(linkModel.get(0).alternateLink)
        onAddedToCollectionFailed: {
            ib.text = "Sorry, somehthing went wrong, please try again!";
            ib.show();
        }
    }

    XmlListModel {
          id: linkModel
          query: "/entry"
          xml: doc_info
          namespaceDeclarations: "declare default element namespace 'http://www.w3.org/2005/Atom'; declare namespace gd = 'http://schemas.google.com/g/2005'; declare namespace docs = 'http://schemas.google.com/docs/2007';"
          XmlRole { name: "link"; query: "content/@src/string()" }
          XmlRole { name: "contentType"; query: "content/@type/string()" }
          XmlRole { name: "lastModified"; query: "docs:modifiedByMeDate/string()" }
          XmlRole { name: "author"; query: "author/name/string()" }
          XmlRole { name: "description"; query: "docs:description/string()" }
          XmlRole { name: "size"; query: "docs:size/number()" }
          XmlRole { name: "resourceId"; query: "gd:resourceId/string()" }
          XmlRole { name: "documentType"; query: "category[@scheme='http://schemas.google.com/g/2005#kind']/@label/string()" }
          XmlRole { name: "self_link"; query: "link[@rel='self']/@href/string()" }
          XmlRole { name: "alternateLink"; query: "link[@rel='alternate']/@href/string()" }
          XmlRole { name: "parentCollection1"; query: "link[@rel='http://schemas.google.com/docs/2007#parent'][1]/@href/string()" }
          XmlRole { name: "parentCollection2"; query: "link[@rel='http://schemas.google.com/docs/2007#parent'][2]/@href/string()" }
          XmlRole { name: "parentCollection3"; query: "link[@rel='http://schemas.google.com/docs/2007#parent'][3]/@href/string()" }
          XmlRole { name: "parentCollection4"; query: "link[@rel='http://schemas.google.com/docs/2007#parent'][4]/@href/string()" }
          XmlRole { name: "parentCollection5"; query: "link[@rel='http://schemas.google.com/docs/2007#parent'][5]/@href/string()" }
    }

    PageHeader {
        id: pageHeader
        text: doc_name
        anchors.top: parent.top
    }

    InfoBanner {
        id: ib
    }

    BusyIndicator {
        id: listBusy
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        //running: true
    }

    ListView {

        id: docDetails
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.leftMargin: 24
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        model: linkModel
        clip: true
        delegate: Item {

            //anchors.horizontalCenter: parent.horizontalCenter

            Column {
                id: lmText
                anchors.top: parent.top
                anchors.topMargin: 24

                Text {

                    text: "Author:"
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {

                    text: author
                    font.pointSize: 14
                    font.family: "Nokia Pure"
                }
                Text {

                    text: " "
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {

                    text: "Description:"
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {
                    width: docDetails.width - 48
                    wrapMode: Text.WordWrap
                    text: description
                    font.pointSize: 14
                    font.family: "Nokia Pure"
                }
                Text {

                    text: " "
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {

                    text: "Document Type:"
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {
                    width: docDetails.width - 48
                    wrapMode: Text.WordWrap
                    text: documentType
                    font.pointSize: 14
                    font.family: "Nokia Pure"
                }
                Text {

                    text: " "
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }

                Text {

                    text: "Size:"
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {

                    text: Math.round(size / 1024) + " KB"
                    font.pointSize: 14
                    font.family: "Nokia Pure"
                }
                Text {

                    text: " "
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }

                Text {

                    text: "Last Modified:"
                    font.pointSize: 20
                    font.family: "Nokia Pure"
                }
                Text {

                    text: lastModified.substr(0, 10)
                    font.pointSize: 14
                    font.family: "Nokia Pure"
                }
                Text {

                    text: lastModified.substr(lastModified.indexOf("T")+1, 8)
                    font.pointSize: 14
                    font.family: "Nokia Pure"
                }
            }

        }
    }

    QueryDialog {
        id: deleteDialog
        acceptButtonText: "Trash it"
        rejectButtonText: "No thanks"
        message: "Do you want to move "+doc_name+" to the Trash?";
        title: Text { text: "Move to Trash" }
        onAccepted: {
            meeDocs.trashFile(linkModel.get(0).self_link, Meedocs.returnAccessToken());
        }
    }

    QueryDialog {
        id: shareDialog
        acceptButtonText: "Share"
        rejectButtonText: "No thanks"
        message: "When you share the document, everybody with the link can view it.";
        title: Text { text: "Share document" }
        onAccepted: {
            meeDocs.shareDoc(linkModel.get(0).resourceId, Meedocs.returnAccessToken());
        }
    }

    ListModel {
        id: downloadChoiceModel
        ListElement { name: "PDF" }
        ListElement { name: "Open Document" }
    }

    SelectionDialog {
         id: downloadDialog
         titleText: "Choose a download format"
         selectedIndex: 0

         onAccepted: {
             var link = linkModel.get(0).link;
             var fileName = doc_name;
             var contentType = linkModel.get(0).contentType;
             var accessToken = Meedocs.returnAccessToken();

             var documentType = linkModel.get(0).documentType;

             if (documentType === "document") {
                 if (selectedIndex === 1) {
                     fileName = fileName + ".odt";
                     link = link + "&exportFormat=odt"
                 } else if (selectedIndex === 2) {
                     fileName = fileName + ".doc";
                     link = link + "&exportFormat=doc"
                 } else if (selectedIndex === 0) {
                     fileName = fileName + ".pdf";
                     link = link + "&exportFormat=pdf"
                 }
             }


             if (documentType === "spreadsheet") {
                 if (selectedIndex === 1) {
                     fileName = fileName + ".ods";
                     link = link + "&exportFormat=ods"
                 } else if (selectedIndex === 2) {
                     fileName = fileName + ".xls";
                     link = link + "&exportFormat=xls"
                 } else if (selectedIndex === 0) {
                     fileName = fileName + ".pdf";
                     link = link + "&exportFormat=pdf"
                 }
             }
             meeDocs.doDownload(link, accessToken, fileName, contentType);
             appWindow.pageStack.pop();
         }

         model: downloadChoiceModel
     }

    ToolBarLayout {
        id: docTools
        visible: false
        ToolIcon {
            platformIconId: "toolbar-back"
            //anchors.left: (parent === undefined) ? undefined : parent.left
            onClicked: {                
                appWindow.pageStack.pop();
            }
        }
        ToolIcon {
            platformIconId: "toolbar-directory-move-to"
            //anchors.left: (parent === undefined) ? undefined : parent.left
            onClicked: {
                var link = linkModel.get(0).link;
                var fileName = doc_name;
                var contentType = linkModel.get(0).contentType;
                var accessToken = Meedocs.returnAccessToken();

                var documentType = linkModel.get(0).documentType;
                if (downloadChoiceModel.get(2))
                    downloadChoiceModel.remove(2);

                if (documentType === "document") {
                    downloadChoiceModel.append({ "name": "Microsoft Word"});
                    downloadDialog.open();
                } else if (documentType === "spreadsheet") {
                    downloadChoiceModel.append({ "name": "Microsoft Excel"});
                    downloadDialog.open();
                } else if (documentType === "presentation") {
                    fileName = fileName + ".pdf"
                    link = link + "&exportFormat=pdf"
                    meeDocs.doDownload(link, accessToken, fileName, contentType);
                    appWindow.pageStack.pop();
                } else {
                    meeDocs.doDownload(link, accessToken, fileName, contentType);
                    appWindow.pageStack.pop();
                }
            }
        }
        ToolIcon {
            platformIconId: "toolbar-share"
            //anchors.left: (parent === undefined) ? undefined : parent.left
            onClicked: {
                //console.log(doc_info);
                /* console.log(linkModel.get(0).resourceId);
                Meedocs.getAcl(linkModel.get(0).resourceId);
                console.log(linkModel.get(0).parentCollection1);
                console.log(linkModel.get(0).parentCollection2);
                console.log(linkModel.get(0).parentCollection3);
                console.log(linkModel.get(0).parentCollection4);
                console.log(linkModel.get(0).parentCollection5); */
                shareDialog.open();
            }
        }
        ToolIcon {
            platformIconId: "toolbar-delete"
            //anchors.left: (parent === undefined) ? undefined : parent.left
            onClicked: {
                deleteDialog.open();
            }
        }
        ToolIcon {
            platformIconId: "toolbar-view-menu"
            //anchors.right: (parent === undefined) ? undefined : parent.right
            onClicked: (myMenu.status === DialogStatus.Closed) ? myMenu.open() : myMenu.close()
        }
    }

}
